package com.zuohuaipan.headine;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.igexin.sdk.PushManager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.zuohuaipan.headine.fragment.Home_Fragment;
import com.zuohuaipan.headine.fragment.Love_Fragment;
import com.zuohuaipan.headine.fragment.My_Fragment;
import com.zuohuaipan.headine.fragment.Video_Fragment;


public class MainActivity extends SlidingFragmentActivity implements View.OnClickListener {


    private ImageView iv_home;
    private TextView home_text;
    private ImageView iv_care;
    private TextView care_text;
    private ImageView iv_video;
    private TextView video_text;
    private ImageView iv_login;
    private TextView login_text;
    private TextView[] texts;
    private Fragment fmg;
    private Home_Fragment home_fragment;
    private LinearLayout newsHome;
    private LinearLayout newsVideo;
    private LinearLayout newsCare;
    private LinearLayout newsLogin;
    public SlidingMenu menu;
    private My_Fragment my_fragment;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    private Video_Fragment video_fragment;
    private Love_Fragment love_fragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // com.getui.demo.DemoPushService 为第三方自定义推送服务
        PushManager.getInstance().initialize(this.getApplicationContext(), DemoPushService.class);
        // com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);

        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();


        initMenu();
        //找到控件
        initView();
        //监听
        setonListener();
        //默认为推荐页面
        setLayout();


    }


    private void setLayout() {
        //默认为推荐
        if (home_fragment == null) {
            home_fragment = new Home_Fragment();
        }
        setFragment(home_fragment);
        setImage(0);
        setTextColor(0);

    }

    /**
     * 监听
     */
    private void setonListener() {
        newsHome.setOnClickListener(this);
        newsVideo.setOnClickListener(this);
        newsCare.setOnClickListener(this);
        newsLogin.setOnClickListener(this);
    }

    private void initView() {
        newsHome = (LinearLayout) findViewById(R.id.newshome);
        newsVideo = (LinearLayout) findViewById(R.id.newsvideo);
        newsCare = (LinearLayout) findViewById(R.id.newscare);
        newsLogin = (LinearLayout) findViewById(R.id.newslogin);
        iv_home = (ImageView) findViewById(R.id.iv_home);
        home_text = (TextView) findViewById(R.id.home_text);
        iv_care = (ImageView) findViewById(R.id.iv_care);
        care_text = (TextView) findViewById(R.id.care_text);
        iv_video = (ImageView) findViewById(R.id.iv_video);
        video_text = (TextView) findViewById(R.id.video_text);
        iv_login = (ImageView) findViewById(R.id.iv_login);
        login_text = (TextView) findViewById(R.id.login_text);
        texts = new TextView[]{home_text, video_text, care_text, login_text};

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.newshome:
                setImage(0);
                setTextColor(0);
                if (home_fragment == null) {
                    home_fragment = new Home_Fragment();
                }
                setFragment(home_fragment);

                break;
            case R.id.newsvideo:
                if (video_fragment == null) {
                    video_fragment = new Video_Fragment();
                }
                setFragment(video_fragment);
                setImage(1);
                setTextColor(1);
                break;
            case R.id.newscare:
                if (love_fragment == null) {
                    love_fragment = new Love_Fragment();
                }
                setFragment(love_fragment);

                setImage(2);
                setTextColor(2);
                break;
            case R.id.newslogin:
                setImage(3);
                setTextColor(3);
                if (my_fragment == null) {
                    my_fragment = new My_Fragment();
                }
                setFragment(my_fragment);
                break;

        }

    }

    public void setFragment(Fragment fragment) {
        //得到事务管理类
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fmg != null) {
            transaction.hide(fmg);
        }
        if (!fragment.isAdded()) {
            transaction.add(R.id.ll, fragment);
        }
        transaction.show(fragment);
        transaction.commit();
        fmg = fragment;


    }

    /**
     * 改变字体颜色
     */
    public void setTextColor(int index) {
        for (int i = 0; i < texts.length; i++) {
            if (i == index) {
                texts[i].setTextColor(Color.parseColor("#f85959"));
            } else {
                texts[i].setTextColor(Color.BLACK);
            }

        }
    }

    /**
     * 改变图片
     */
    public void setImage(int id) {
        switch (id) {
            case 0:
                iv_home.setImageResource(R.mipmap.b_newhome_tabbar_press);
                iv_care.setImageResource(R.mipmap.b_newcare_tabbar);
                iv_video.setImageResource(R.mipmap.b_newvideo_tabbar);
                iv_login.setImageResource(R.mipmap.b_newnologin_tabbar);
                break;
            case 1:
                iv_video.setImageResource(R.mipmap.b_newvideo_tabbar_press);
                iv_home.setImageResource(R.mipmap.b_newhome_tabbar);
                iv_care.setImageResource(R.mipmap.b_newcare_tabbar);
                iv_login.setImageResource(R.mipmap.b_newnologin_tabbar);
                break;
            case 2:
                iv_care.setImageResource(R.mipmap.b_newcare_tabbar_press);
                iv_home.setImageResource(R.mipmap.b_newhome_tabbar);
                iv_video.setImageResource(R.mipmap.b_newvideo_tabbar);
                iv_login.setImageResource(R.mipmap.b_newnologin_tabbar);
                break;
            case 3:
                iv_login.setImageResource(R.mipmap.b_newnologin_tabbar_press);
                iv_home.setImageResource(R.mipmap.b_newhome_tabbar);
                iv_care.setImageResource(R.mipmap.b_newcare_tabbar);
                iv_video.setImageResource(R.mipmap.b_newvideo_tabbar);
                break;
        }
    }

    /**
     * 初始化侧滑菜单
     */
    private void initMenu() {
        //第一步
        setBehindContentView(R.layout.menu_layout);


        //第二步获得对象,设置属性
        menu = getSlidingMenu();
        //设置侧滑阴影宽度
        menu.setShadowWidthRes(R.dimen.shadow_width);
        //设置侧滑时背景颜色
        menu.setShadowDrawable(R.drawable.shadow);
        //设置屏幕剩余的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        //设置阴影渐变程度
        menu.setFadeDegree(0.35f);
        //设置滑动屏幕范围
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        //设置滑动时是否淡入淡出
        menu.setFadeEnabled(true);


    }

}
